package com.hsav.kcxb.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.media.Image;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hsav.kcxb.R;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyTools.ACloudType;
import com.kcbsdk.MyTools.AIconType;
import com.kcbsdk.MyTools.AIconTypeList;
import com.zhy.autolayout.utils.AutoUtils;

import static com.hsav.kcxb.main.MSYS.mMyTools;

public class ClassListActivity extends AppCompatActivity {

    private RecyclerView mClassListRecycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_list);
        initKcbData();
        initView();
    }
    private void initKcbData() {          // 数据接口回调函数
        if (mMyTools != null){
            mMyTools.setUiEventCallBack(this, new EventHandler() {
                @Override
                public void onComplete(int methodType, boolean isFail, String message) {
                    if (methodType > 0){
                        if (isFail){
                            mMyTools.Toast(String.format("%s%s(%08x)", getString(R.string.methodError), message, methodType));
                        }
                        else {
                            if (mMyTools.isSucessIconTypeListDbs(methodType, AIconType.ICON_OBB_CLASS)){
                                String gsLocal_1 = "伴奏分类列表：";
                                if (null != mMyTools.mAIconTypeList) {
                                    AIconTypeList aIconTypeList;
                                    for (int counter = 0; counter < mMyTools.mAIconTypeList.length; counter++) {
                                        aIconTypeList = mMyTools.mAIconTypeList[counter];
                                        gsLocal_1 += String.format(" TypeName：%s", aIconTypeList.TypeName);
                                        gsLocal_1 += String.format(" IconFile：%s", aIconTypeList.IconFile);
                                        gsLocal_1 += String.format(" PinYin：%s", aIconTypeList.PinYin);
                                    }
                                }
                                mMyTools.Toast(gsLocal_1);
                            }
                        }
                    } else{
                    }
                }
            });
            mMyTools.fetchIconTypeList(ACloudType.TYPE_KBA, AIconType.ICON_OBB_CLASS);	// 伴奏分类列表及图标
        }
    }

    private void initView() {
        TextView titleTx = (TextView)findViewById(R.id.tv_top_title);
        titleTx.setText("分类");
        ImageView backImg = (ImageView)findViewById(R.id.iv_top_back);
        backImg.setVisibility(View.VISIBLE);

        mClassListRecycler = (RecyclerView)findViewById(R.id.recycler_class_list);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
        ClassAdapter classAdapter = new ClassAdapter(this);
        mClassListRecycler.addItemDecoration(new MyItemDecoration(AutoUtils.getPercentWidthSize(8)));
        mClassListRecycler.setLayoutManager(gridLayoutManager);
        mClassListRecycler.setAdapter(classAdapter);
    }

    private class ClassAdapter extends RecyclerView.Adapter implements View.OnClickListener {

        private final Context mContext;
        private final LayoutInflater mInflater;


        public ClassAdapter(Context context){
            this.mContext = context;
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View rootView = mInflater.inflate(R.layout.item_class_singer,parent,false);
            rootView.setOnClickListener(this);
            return new MyViewHolder(rootView);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((MyViewHolder)holder).setData(position);
        }

        @Override
        public int getItemCount() {
            return 16;
        }

        @Override
        public void onClick(View view) {
            int position = (int)view.getTag();
            Toast.makeText(mContext, String.format("position: %s", position), Toast.LENGTH_SHORT).show();
            startActivity(new Intent(mContext, SingerWorkActivity.class));
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{

        private final ImageView mPicImg;
        private final TextView mNameTx;

        public MyViewHolder(View itemView) {
            super(itemView);
            mPicImg = (ImageView)itemView.findViewById(R.id.img_class_pic);
            mNameTx = (TextView)itemView.findViewById(R.id.tx_class_name);
            AutoUtils.autoSize(itemView);
        }

        public void setData(int position) {
            String title = "华语经典";
            mNameTx.setText(title+position);
            itemView.setTag(position);
        }
    }

    private class MyItemDecoration extends RecyclerView.ItemDecoration{

        private final int mDistance;

        public MyItemDecoration(int distance){
            this.mDistance = distance;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.top = mDistance;
            outRect.left = 0;
            outRect.right = 0;

        }
    }
}
